
exports.GAStoreHandler = Core.Class.subclass ({

	initialize: function(stores, successCallback, failureCallback) {
		this._stores = stores;
		this._successCallback = successCallback;
		this._failureCallback = failureCallback;
		this._totalStores = this._stores.length;
		this._loadCounter = 0;
		this._objects = {};
	},
	
	startLoading: function() {
		Storage.FileSystem.readFile(this.getStorePath(), this.loadComplete.bind(this));
	},
	
	getStorePath: function(fileName) {
		var path = [];
		path.push("./Content/stores/");
		path.push(this._stores[this._loadCounter]);
		path.push(".json");
		return path.join("");
	},
	
	loadComplete: function(error, contents) {
		var visibleId = this._stores[this._loadCounter];	
		if (!error) {
			var obj = eval("(" + contents + ")");
			if (typeof this._companyId == 'undefined') {
				this._companyId = obj.company.id;
			}
			if (typeof this._gameId == 'undefined') {
				this._gameId = obj.company.game.id;
			}
			//console.log("zaki:::: companyID is "+this._companyId+"   and gameId is "+this._gameId);
			var store = obj.company.game.store;
			//console.log("zaki::::: obj.company.game.store"+store);
			this.setParentReferences(store);
			this._objects[visibleId + ""] = store;
			this._loadCounter++;
			if (this._loadCounter < this._totalStores) {
				Storage.FileSystem.readFile(this.getStorePath(), this.loadComplete.bind(this));
			}
			else {
				this._successCallback();
			}
		}
		else {
			this._failureCallback(error, visibleId);
		}
	},
	
	setParentReferences: function(store) {
		store.parent = null;
		store.objectType = "store";
		var cats = store.categories;
		if (typeof cats != 'undefined') {
			var totalCats = cats.length;
			for (var i = 0; i < totalCats; i++) {
				var cat = cats[i];
				cat.parent = store;
				cat.objectType = "cat";
				var items = cat.items;
				var totalItems = items.length;
				if (typeof items != 'undefined') {
					for (var j = 0; j < totalItems; j++) {
						items[j].parent = cat;
						items[j].objectType = "item";
					}
					cat.activeItems = items.filter(this.keepObject.bind(this));
				}
			}
			store.activeCategories = cats.filter(this.keepObject.bind(this));
		}
	},
	
	keepObject: function(storeObject) {	// return true if we'd like to keep this object (and NOT filter it)
		var gameVersion = GAConfigurations.getGameVersion();
		var isQAUser = GAGameLoader.getResponseObject().isQAUser();
		var minVersion = (typeof storeObject.version_min != 'undefined') ? storeObject.version_min : 0;
		var maxVersion = (typeof storeObject.version_max != 'undefined') ? storeObject.version_max : gameVersion;
		if (gameVersion >= minVersion && gameVersion <= maxVersion && storeObject.is_active == true) {
			if (isQAUser == false && storeObject.visible_to.toLowerCase() == "qa".toLowerCase()) {
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return false;
		}
	},
	
	getCompanyId: function() {
		return this._companyId;
	},
	
	getGameId: function() {
		return this._gameId;
	},
	
	getStore: function(storeId) {
		var store = this._objects[storeId];
		if (typeof store != 'undefined') {
			return store;
		}
		return null;
	},
	
	getCategory: function(storeId, catId, active) {
		var store = this._objects[storeId];
		//console.log("zaki::::: store is "+(this._objects[storeId]));
		if (typeof store != 'undefined') {
			var categories = (active == true) ? store.activeCategories : store.categories;
			//console.log("zaki:::: categories are "+categories);
			if (typeof categories != 'undefined') {
				var totalCats = categories.length;
				for (var i = 0; i < totalCats; i++) {
					if (categories[i].visible_id == catId) {
						return categories[i];
					}
				}
			}
			return null;
		}
		return null;
	},
	
	getCategoryByIndex: function(storeId, catIndex, active) { 
		//catIndex is the position of a category within the categories array.
		var store = this._objects[storeId];
		if (typeof store != 'undefined') {
			var categories = (active == true) ? store.activeCategories : store.categories;
			if (typeof categories != 'undefined') {
				return categories[catIndex];
			}
			return null;
		}
		return null;
	},
	
	getItem: function(storeId, catId, itemId, active) {
		var category = this.getCategory(storeId, catId, active);
		//console.log("zaki::::: category id is "+category);
		if (category != null) {
			var items = (active == true) ? category.activeItems : category.items;
			//console.log("zaki::::: items are "+items);
			if (typeof items != 'undefined') {
				var totalItems = items.length;
				for (var i = 0; i < totalItems; i++) {
					if (items[i].visible_id == itemId) {
						return items[i];
					}
				}
			}
			return null;
		}
		return null;
	},
	
	getItemByIndex: function(storeId, catIndex, itemIndex, active) { 
		//catIndex & itemIndex are the positions of a category and an item within the categories & items array respectively.
		var category = this.getCategory(storeId, catIndex, active);
		if (category != null) {
			var items = active == true ? category.activeItems : category.items;
			if (typeof items != 'undefined') {
				return items[itemIndex];
			}
			return null;
		}
		return null;
	},
	
	getStoreObject: function(storeId,catId,itemId)
	{
		//console.log("zaki:::: item id is "+itemId);
		if(itemId != null)
		{
			return this.getItem(storeId, catId, itemId, false);
		}
		if(catId != null)
		{
			return this.getCategory(storeId, catId, false);
		}
		if(storeId != null)
		{
			return this.getStore(storeId);
		}
		return null;
	},
	
	getAllCategories: function(storeId, active) {
		var store = this._objects[storeId];
		if (typeof store != 'undefined') {
			return (active == true ? store.activeCategories : store.categories);
		}
		return null;
	},
	
	getAllItems: function(storeId, catId, active) {
		var category = this.getCategory(storeId, catId, active);
		if (category != null) {
			return (active == true ? category.activeItems : category.items);
		}
		return null;
	},
	
	getAllItemsByCategoryIndex: function(storeId, catIndex, active) {
		var category = this.getCategoryByIndex(storeId, catIndex, active);
		if (category != null) {
			return (active == true) ? category.activeItems : category.items;
		}
		return null;		
	},
	
	releaseObject: function(store) {
		var categories = store.activeCategories;
		var totalCats = categories.length;
		for (var i = 0; i < totalCats; i++) {
			delete categories[i].activeItems;
		}
		delete store.activeCategories;
	},
	
	getStoreObjectForKey: function(key)
	{
		if(key != null)
		{
			var storeKeys = key.split("_");
			//console.log("zaki::::: store keys are "+storeKeys+ "   store keys length "+storeKeys.length);
			if(storeKeys.length == 1)//store
			{
				return this.getStoreObject(storeKeys[0]);
				//console.log("Bhai Store");
				return null;
			}
			else if(storeKeys.length == 2)//category
			{
				return this.getStoreObject(storeKeys[0], storeKeys[1]);
				//console.log("Bhai Cat");
				return null;
			}
			else if(storeKeys.length == 3)//storeitem
			{
				return this.getStoreObject(storeKeys[0], storeKeys[1], storeKeys[2]);
				//console.log("Bhai StoreItem");
				return null;
			}
			else//invalid key
			{
				//console.log("Bhai null");
				return null;
			}
		}
		//console.log("Bhai null");
		return null;
	},
	
	destroy: function() {
		for (var i in this._objects) {
			this.releaseObject(this._objects[i]);
			this._objects[i] = null;
		}
		this._objects = null;
		this._stores = null;
		this._successCallback = null;
		this._failureCallback = null;
		this._delegate = null;
		this._storesCounter = null;
		this._totalStores = null;
		this._gameId = null;
		this._companyId = null;
	}
});